const {
  getLeaveMessage,
  addLeaveMessage,
  updateLeaveMessage,
  deleteLeaveMessage,
  likeLeaveMessage,
} = require("../../service/leave_message/leave_message.service");
const {
  querySuccess,
  addSuccess,
  updateSuccess,
  deleteSuccess,

} = require("../../constant/success.type");
const {
  addError,
  queryError,
  updateError,
  deleteError,
} = require("../../constant/err.type");
class LeaveMessageController {
  async getLeaveMessage(ctx, nuxt) {
    ctx.set("Cache-Control", "no-cache");
    try {
      const db_response = await getLeaveMessage(ctx.request.query);
      querySuccess.result = db_response;
      ctx.body = querySuccess;
    } catch (err) {
      console.log(err);
      ctx.app.emit("error", queryError, ctx);
    }
  }
  async addLeaveMessage(ctx, nuxt) {
    const { id } = ctx.state.user;
    const user_id = { user_id: id };
    try {
      const db_response = await addLeaveMessage({
        ...ctx.request.body,
        ...user_id,
      });
      addSuccess.result = db_response;
      ctx.body = addSuccess;
    } catch (err) {
      console.log(err);
      ctx.app.emit("error", addError, ctx);
    }
  }
  async updateLeaveMessage(ctx, nuxt) {
    try {
      const db_response = await updateLeaveMessage(ctx.request.body);
      updateSuccess.result = db_response;
      ctx.body = updateSuccess;
    } catch (err) {
      console.log(err);
      ctx.app.emit("error", updateError, ctx);
    }
  }
  async likeLeaveMessage(ctx, nuxt) {
    try {
      const db_response = await likeLeaveMessage(ctx.request.body);
      updateSuccess.result = db_response;
      ctx.body = updateSuccess;
    } catch (err) {
      console.log(err);
      ctx.app.emit("error", updateError, ctx);
    }
  }
  async deleteLeaveMessage(ctx, nuxt) {
    try {
      const db_response = await deleteLeaveMessage(ctx.query);
      deleteSuccess.result = db_response;
      ctx.body = deleteSuccess;
    } catch (err) {
      console.log(err);
      ctx.app.emit("error", deleteError, ctx);
    }
  }
}

module.exports = new LeaveMessageController();
